package example.interceptor;

import example.utils.JwtUtil;
import example.utils.Jwts_JWTUtil;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JwtInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Jwts_JWTUtil jwts_jwtUtil;

    /*@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截了");
        String token = request.getHeader("Authorization");
        if(!"".equals(token) && token != null){
            try{
                Claims claims = jwtUtil.parseJWT(token);
                request.setAttribute("loginUser", claims);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("令牌有误请登录 --catch-- !!!!!!");
                throw new RuntimeException("令牌有误请登录 --catch-- !!!!!!");
            }
        }else{
            System.out.println("令牌有误请登录 --else-- !!!!!!");
            throw new RuntimeException("令牌有误请登录 --else-- !!!!!!");
        }
        return true;
    }*/


//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("拦截了");
//        String token = request.getHeader("Authorization");
//        if ("".equals(token) || null == token) {
//            throw new RuntimeException("令牌有误请登录 --browser没有token-- !!!!!!");
//        }
//        String refToken = (String) redisTemplate.opsForValue().get(token);
//        if (refToken == null || "".equals(refToken)) {
//            throw new RuntimeException("令牌有误请登录 --redis没有token-- !!!!!!");
//        }
//        try {
//            Claims claims = jwtUtil.parseJWT(token);
//            String refToken2 = jwtUtil.createJWT(claims.getId(), claims.getSubject(), (String) claims.get("roles"));
//            redisTemplate.opsForValue().set(token, refToken2, 1, TimeUnit.MINUTES);
//            request.setAttribute("loginUser", claims);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("令牌有误请登录 --catch-- !!!!!!");
//        }
//        return true;
//    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");
        System.out.println("拦截了");
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        if (StringUtils.equalsIgnoreCase("/zn/login", request.getRequestURI())) {
            return true;
        }
        String token = request.getHeader("Authori-zation");
        if (StringUtils.isBlank(token)) {
            throw new RuntimeException("令牌有误请登录 --browser没有token-- !!!!!!");
        }
        try {
            if (!jwts_jwtUtil.isTokenExpired(token)) {
                throw new RuntimeException("登录过期，请重新登录-- !!!!!!");
            }
            Claims claims = jwts_jwtUtil.parseToken(token);
            request.setAttribute("loginUser", claims);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        return true;
    }
}
